<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.URL"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>

<%@page import="java.io.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="rnd.mock.UserDetail"%>

<%@taglib uri="http://www.javaPractise.org/jsp/tags/pagination" prefix="p" %>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>POJO Pagination + Dynamic Text Search</title>
		
		<script type="text/javascript" src="../js-lib/jquery/core/jquery-1.6.2.js"></script>
		<script type="text/javascript" src="../DynamicSearch/plugin/dynamic-search-plugin.js"></script>
		<script type="text/javascript" src="../ComponentReposition/plugin/component-reposition-plugin.js"></script>
		
		<link rel="stylesheet" type="text/css" href="style/style.css" />
	</head>
	<body>
		<div class="info">
			* Press Ctrl + Alt + S to show search box.<br/>
			* Press Esc to close the search box.
		</div>
		<%
			Integer p = 1;
			Integer rpp = 15;
			ArrayList<Object> userDetails = new ArrayList<Object>();
			
			if(request.getParameter("p") != null) {
				p = Integer.parseInt(request.getParameter("p"));
			}
			if(request.getParameter("rowsPerPage") != null) {
				rpp = Integer.parseInt(request.getParameter("rowsPerPage"));
			}
			
			try {
		        String line = null;
		        String filePath =request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();

				URL url = new URL( filePath + "/resources/cricketers.txt" );
				InputStream is = new BufferedInputStream(url.openStream());
				Reader reader = new InputStreamReader(is, "UTF-8");
				BufferedReader bufferedReader = new BufferedReader(reader);
		        try {
		        	int counter = 1;
		            while ((line = bufferedReader.readLine()) != null) {
		            	UserDetail userDetail = new UserDetail();
		            	String[] info = line.split(":");
						userDetail.setId(counter);
						userDetail.setName(info[0]);
						userDetail.setEmail(info[1]);
						
						userDetails.add(userDetail);
		            	counter++;
		            }
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		            	bufferedReader.close();
		                reader.close();
		                is.close();
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
			} catch (IOException e) {
		        e.printStackTrace();
			}
		%>
		<table class="contentTable">
			<p:customPojoPaginated currentPage="<%=p%>" uri="<%=request.getRequestURI()%>" collection="<%=userDetails%>" queryString="<%=request.getQueryString()%>" headerStyle="header-bg" rowStyle="row-bg" rowCountRequired="true" pageMarkupStyle="pageMarkup" rowsPerPage="<%=rpp %>"></p:customPojoPaginated>
			<tr>
				<td colspan="3" style="font-size: 12px; font-family: verdana;">
					Rows Per Page:
					<select id="rows-per-page" name="rowsPerPage">
						<option value="5">5</option>
						<option value="10">10</option>
						<option value="15">15</option>
						<option value="20">20</option>
						<option value="25">25</option>
						<option value="30">30</option>
					</select>
				</td>
			</tr>
		</table>
	</body>
	
	<script>
		jQuery( document ).ready(function() {
			
			var config = {
				relativeTo: '.contentTable', // selector of the content container - id or class definition
				searchCmpId: 'searchCmp',
				searchBoxPosition: 'bottom',
				searchBoxMargins: "2,-1"
			};
				
			dynamicSearch.initialize( config );
			
			$( '#rows-per-page' ).change(function() {
				window.location = window.location.pathname + '?rowsPerPage=' + $( this ).val();
			});
			
			$( '#rows-per-page' ).val('<%=rpp%>');
		});
	</script>
</html>